package com.keehin;

import java.awt.Font;

import javax.swing.*;
import javax.swing.table.*;

public class DBTable extends JTable {

	private static final long serialVersionUID = 1L;

	private static final MyRenderer renderer = new MyRenderer();

	private static final Font FONT = new Font("Tahoma", Font.PLAIN, 14);

	@Override
	public void setModel(TableModel dataModel) {
		if (dataModel instanceof DBTableModel) {
			DBTableModel model = (DBTableModel) dataModel;
			if (model.isChecked())
				super
						.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
			else
				super.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		}
		super.setModel(dataModel);
		this.resizeWidths();
		this.repaint();
		this.setRowHeight(22);
		// this.setBackground(Color.BLACK);
		// this.setForeground(Color.LIGHT_GRAY );
		this.setShowHorizontalLines(false);
		this.setFont(DBTable.FONT);
	}

	private int[] widths;

	private void resizeWidths() {
		if (widths != null) {
			TableColumn column;
			int sum = 0;
			for (int i = 0; i < widths.length && i < this.getColumnCount(); i++) {
				column = this.getColumnModel().getColumn(i);
				column.setPreferredWidth(widths[i]);
				sum += widths[i];
			}
			// this.setPreferredSize(new Dimension(sum, ));
			for (int i = 1; i < this.getColumnCount(); i++) {
				String cname = this.getColumnClass(i).getName();

				if (cname.equals("java.util.Date")
						|| cname.equals("java.lang.Integer")
						|| cname.equals("java.lang.Float")) {
					column = this.getColumnModel().getColumn(i);
					column.setCellRenderer(DBTable.renderer);
				}
			}
		}
	}

	public void setWidths(int[] widths) {
		this.widths = widths;
		this.resizeWidths();
	}
}
